package com.example.androidbighomework.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.androidbighomework.LoginActivity;
import com.example.androidbighomework.MainActivity;
import com.example.androidbighomework.R;
import com.example.androidbighomework.SQLiteHelper.MyHelper;
import com.example.androidbighomework.adapter.myBaseAdapter;

import static android.content.ContentValues.TAG;


public class HomeFragment extends Fragment {
    private LayoutInflater inflater;
    private View view;
    private ListView mListViewHome;
    //登录按钮
    private Button mbtn_main_login,mbtn_main_logout;
    @SuppressLint("WrongConstant")
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.activity_home_fragment, container, false);
        if(view!=null){
            //初始化控件
            initView();
        }

        final Integer accountid= ((MainActivity)getActivity()).getAccountId();
        if(accountid!=null){
            mbtn_main_login.setVisibility(4);
            mbtn_main_logout.setVisibility(0);
        }

        MyHelper myHelper=new MyHelper(getContext());
        myHelper.find();
        //实例化适配器
        myBaseAdapter adapter=new myBaseAdapter();
//        mListViewHome.requestLayout();
//        adapter.notifyDataSetChanged();
        mListViewHome.setAdapter(adapter);
        mbtn_main_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getActivity(), LoginActivity.class);
                startActivity(intent);
            }
        });
        mbtn_main_logout.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("WrongConstant")
            @Override
            public void onClick(View v) {
                ((MainActivity)getActivity()).setAccountId(null);
                mbtn_main_login.setVisibility(0);
                mbtn_main_logout.setVisibility(4);
                Toast.makeText(getContext(), "退出登录成功", Toast.LENGTH_SHORT).show();
            }
        });
        isMainLoop();
        return view;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);

    }

    //初始化控件
    private void initView() {
        mListViewHome=view.findViewById(R.id.list_view_home);
        mbtn_main_login=view.findViewById(R.id.btn_main_login);
        mbtn_main_logout=view.findViewById(R.id.btn_main_logout);
    }


    //判断是否在主线程
    public static boolean isMainLoop() {
        boolean ret;
        if (Looper.getMainLooper() == Looper.myLooper()) {
            Log.i(TAG, "I am MainLooper");
            ret = true;
        } else {
            Log.i(TAG, "I am NoLooper");
            ret = false;
        }
        return ret;
    }
}